package com.ruoyi.web.controller.sujian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SjElecCost;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.service.IElecCostService;
import com.ruoyi.system.service.ISysFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @date 2021-07-05
 */
@Controller
@RequestMapping("/sujian/elecCost")
public class ElecCostController extends BaseController
{
    private String prefix = "propertys/sujian/elec/cost";

    @Autowired
    private ISysFloorService floorInfoService;

    @Autowired
    private IElecCostService elecCostService;

    @GetMapping()
    public String electricityCost(ModelMap modelMap)
    {
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        modelMap.put("floorInfos",floorInfos);
        return prefix + "/main";
    }

    /**
     * 查询员工考勤列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjElecCost sjElecCost)
    {
        startPage();
        List<SjElecCost> sjElecCostList = elecCostService.selectElecCostList(sjElecCost);
        return getDataTable(sjElecCostList);
    }

    @PostMapping("/generateElec")
    @ResponseBody
    public AjaxResult generateElec(String etldt) {
        //查询该批次是否存在
        SjElecCost sjElecCost = new SjElecCost();
        etldt = etldt.replaceAll("-","");
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMM");
        try {
            Date date = sdfDate.parse(etldt);
        } catch (ParseException e) {
            return AjaxResult.error("日期格式不正确");
        }
        sjElecCost.setEtldt(etldt);
        List<SjElecCost> sjElecCostList = elecCostService.selectElecCostList(sjElecCost);
        if(sjElecCostList!=null){
            //删除之前数据  重新生成？
            elecCostService.deleteElecByEtldt(etldt);
        }
        List<SysFloor> floorInfos = floorInfoService.selectSysFloorListAll(new SysFloor());
        if(floorInfos!=null&&floorInfos.size()>0){
            elecCostService.generateElec(etldt,floorInfos);
        }else{
            return AjaxResult.error("未维护楼层");
        }
        return AjaxResult.success();
    }


    //导入电费数据
    @PostMapping("/importExcel")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SjElecCost> util = new ExcelUtil<>(SjElecCost.class);
        List<SjElecCost> lists = util.importExcel(file.getInputStream());
        //校验数据
        String message =elecCostService.importElecCost(lists);
        return AjaxResult.success(message);
    }


    //导入电费数据模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SjElecCost> util = new ExcelUtil<SjElecCost>(SjElecCost.class);
        return util.importTemplateExcel("电费信息数据");
    }

    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjElecCost sjElecCost) {
        List<SjElecCost> list = elecCostService.exportElecCost(sjElecCost);
        ExcelUtil<SjElecCost> util = new ExcelUtil(SjElecCost.class);
        return util.exportExcel(list, "电费数据");
    }
}
